Skip to content

Fix byte order of port in endpoint2hostname of IPv6 endpoint - #3736

Open
0z5a wants to merge 1 commit into
apache:masterfrom
0z5a:fix/ipv6-endpoint2hostname-port
Open

0z5a wants to merge 1 commit into
apache:masterfrom
0z5a:fix/ipv6-endpoint2hostname-port

Conversation

@0z5a

@0z5a 0z5a commented Nov 11, 2026

Copy link
Copy Markdown

What problem does this PR solve?

Issue Number: resolve #3734

Problem Summary:

butil::endpoint2hostname() of an IPv6 endpoint prints the port in network byte order. [::1]:8080 gives ip6-localhost:36895 on little-endian machines, while endpoint2str() and the IPv4 path print 8080.

ExtendedEndPoint::to_hostname() formats _u.in6.sin6_port without ntohs(); ExtendedEndPoint::to(EndPointStr*) already converts it. Inside bRPC the wrong port shows on /connections for IPv6 peers when -show_hostname_instead_of_ip is on.

What is changed and the side effects?

Changed:

  • ExtendedEndPoint::to_hostname() prints ntohs(_u.in6.sin6_port).
  • New test EndPointTest.ipv6_endpoint2hostname_port. It fails on master (ip6-localhost:36895) and passes with this change. It is skipped when ::1 has no reverse name on the machine.
  • Tested: test_butil --gtest_filter='EndPointTest.*' passes (23 tests) with gcc 13.3, CMake build, on Linux x86_64. The rest of test_butil, the make and bazel builds, clang and macOS were not run locally.
  • AI disclosure: Claude Code, model claude-fable-5-2; AI was used to find the bug, write the reproducer, the fix and the test, run the tests listed above, and write the first draft of this description; AI-generated code: the ntohs() call in src/butil/details/extended_endpoint.hpp and the new test in test/endpoint_unittest.cpp. The commit carries Generated-by: as the ASF generative tooling guidance recommends.

Side effects:

  • Performance effects: none.

  • Breaking backward compatibility: none, apart from the port now being the real one.


Check List:

🤖 Generated with Claude Code

ExtendedEndPoint::to_hostname() printed sin6_port without ntohs(), so
endpoint2hostname() of [::1]:8080 gave "localhost:36895" on little
endian machines, while endpoint2str() and the IPv4 path print 8080.
/connections shows the wrong port for IPv6 peers when
-show_hostname_instead_of_ip is on.

Generated-by: Claude Code (Claude Fable 5.2)
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Dezhen Lu <dezhen.lu@student.uni-tuebingen.de>
@chenBright
chenBright requested a lite review from Copilot November 11, 2026 11:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

2 open issues
What changed in this PR

Fixes incorrect port formatting for IPv6 endpoints in endpoint2hostname() by converting the stored network-order port to host byte order, and adds a regression test to prevent reintroduction.

Changes:

  • Convert IPv6 port from network byte order via ntohs() when formatting hostnames.
  • Add a unit test covering endpoint2hostname() port formatting for IPv6 loopback.
File Description
test/​endpoint_unittest.cpp Adds regression test asserting IPv6 endpoint2hostname() prints port in host byte order.
src/​butil/​details/​extended_endpoint.hpp Fixes IPv6 hostname formatting to use host-order port.

Review effort: Lite


std::string host_str;
ASSERT_EQ(0, butil::endpoint2hostname(ep, &host_str));
ASSERT_EQ(host, host_str);
size_t len = ::strlen(host);
if (len + 1 < host_len) {
snprintf(host + len, host_len - len, ":%d", _u.in6.sin6_port);
snprintf(host + len, host_len - len, ":%d", ntohs(_u.in6.sin6_port));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

endpoint2hostname() prints a byte-swapped port for IPv6 endpoints

2 participants